<?php


namespace App\Controller\Api\V1;

use App\Common\BeseController;
use App\Model\Mysql\CategoryModel;
use App\Model\Mysql\OrderModel;
use App\Model\Mysql\ProjectModel;
use App\Model\Mysql\ProjectTopicContentModel;
use App\Model\Mysql\ProjectTopicModel;
use App\Model\Mysql\ProjectVideoModel;
use App\Model\Mysql\UserExamineModel;
use App\Model\Mysql\UserLevelModel;
use App\Model\Mysql\UserModel;
use App\Model\Mysql\UserSearchModel;
use App\Model\Mysql\UserVideoModel;

class Project extends BeseController
{

    public function index()
    {
        $input = $this->input;
        $userId = $input['user']['id'] ?? 1;

        $page = empty($input['page']) ? 0 : $input['page'] - 1;
        $size = empty($input['size']) ? 10 : $input['size'];

        $model = ProjectModel::create();

        $where = ['status' => 1];

        $orderBY = ['sort' => 'ASC', 'id' => 'DESC'];

        if (!empty($input['label_id'])) {
            switch ($input['label_id']) {
                case -1:
                    $where['new_status'] = 1;
                    break;
                case -2:
                    $orderBY = array_merge(['integral' => 'DESC'], $orderBY);
                    break;
                case -3:
                    $where['type'] = 1;
                    break;
                default:

                    $model = $model->where("find_in_set({$input['label_id']},label_ids)");
                    break;
            }
        }
        if (!empty($input['category_id'])) {
            $where['category_id'] = $input['category_id'];
        }

        if (!empty($input['name'])) {
            $where['name'] = ["%{$input['name']}%", "LIKE"];
            if (UserSearchModel::getValue(['search' => $input['name'], 'user_id' => $userId])) {
                UserSearchModel::editData(['search_time' => time()], ['user_id' => $userId]);
            } else {
                UserSearchModel::addData(['search' => $input['name'], 'user_id' => $userId, 'search_time' => time()]);
            }
        }
        $model = $model->field(['id', 'name', 'thumb', 'integral', '`describe`', 'new_status', 'type'])->limit($page * $size, $size);


        if (!empty($orderBY)) {
            foreach ($orderBY as $index => $item) {
                $model = $model->order($index, $item);
            }
        }
        $project = $model->all($where);

        return $this->responseSuccess('请求成功', $project);
    }

    public function category()
    {
        $category = CategoryModel::getlist(['status' => 1], ['id as category_id', 'name'], [], ['sort' => 'ASC', 'id' => 'DESC']);

        return $this->responseSuccess('请求成功', $category);
    }

    public function detail()
    {
        $input = $this->input;

        $userId = $input['user']['id'] ?? 1;

        $levelId = UserModel::getValue(['id' => $userId], 'level_id');

        $info = ProjectModel::getOne(['id' => $input['project_id']], ['id', 'name', 'video', '`describe`', 'detail', 'at_level']);

        if (!$info) return $this->responseError('项目不存在');
        $topic = ProjectTopicModel::getColumn(['project_id' => $input['project_id'], 'status' => 1], 'title', ['sort' => 'ASC', 'id' => 'DESC']);
        foreach ($topic as $key => &$value) {
            $value = ++$key . '.' . $value;
        }
        $info['careful_matter'] = $topic;
        $info['examine_status'] = UserExamineModel::getValue(['user_id' => $userId, 'project_id' => $input['project_id']], 'status') ?? 1;
//        $info['video_status'] = UserVideoModel::getValue(['project_id' => $input['project_id'], 'user_id' => $userId]) ? 1 : 0;
        $info['video_status'] = 1;
        $info['order_list'] = OrderModel::getList(['user_id' => $userId, 'project_id' => $input['project_id']], ['id', 'client_name', 'create_time', 'order_status']);
        $info['buy_status'] = 1;
        if ($info['at_level'] > $levelId) {
            $info['buy_status'] = 0;
        }
        $info['system'] = 0;
        $info->hidden(['at_level']);
        return $this->responseSuccess('请求成功', $info);
    }

    public function userVideo()
    {
        $input = $this->input;

        $userId = $input['user']['id'] ?? 1;

        $res = UserVideoModel::getValue(['project_id' => $input['project_id'], 'user_id' => $userId]);

        if ($res) return $this->responseSuccess('操作成功');

        $res = UserVideoModel::addData(['project_id' => $input['project_id'], 'user_id' => $userId]);

        if (!$res) return $this->responseError('操作失败');

        return $this->responseSuccess('操作成功');
    }

    public function moreVideos()
    {
        $input = $this->input;

        $video = ProjectVideoModel::getList(['project_id' => $input['project_id'], 'status' => 1], ['id', 'name', 'video'], ['sort' => 'ASC', 'id' => 'DESC']);

        return $this->responseSuccess('请求成功', $video);

    }

    public function projectCheck()
    {
        $input = $this->input;

        $topic = ProjectTopicModel::getList(['project_id' => $input['project_id'], 'status' => 1], ['id', 'subject', 'type', 'num'], ['sort' => 'ASC', 'id' => 'DESC']);

        $projectName = ProjectModel::getValue(['id' => $input['project_id']], 'name');


        foreach ($topic as &$value) {
            $value['subject'] = $value['subject'] . ($value['type'] ? '(多选)' : '(单选)');

            $value['subject'] = $value['subject'] . '-' . $value['num'] . '分';

            $value['content'] = ProjectTopicContentModel::getColumn(['topic_id' => $value['id']], 'content');

            $value->hidden(['type', 'num']);
        }
        return $this->responseSuccess('请求成功', ['project_name' => $projectName, 'topic' => $topic]);
    }

    public function projectSubmit()
    {
        $input = $this->input;

        $userId = $input['user']['id'] ?? 1;

        $info = ProjectModel::getOne(['id' => $input['project_id']], ['id', 'num']);

        if (!$info) return $this->responseError('项目不存在');

        $examine = UserExamineModel::getOne(['user_id' => $userId, 'project_id' => $input['project_id']], ['id', 'status']);

        if (!empty($examine) && $examine['status'] == 2) return $this->responseError('您已经通过考核，请不要重复操作');

        $data = json_decode($input['data'], JSON_UNESCAPED_UNICODE);

        if (!is_array($data)) return $this->responseError('data 不是json格式');
        $num = 0;
        $numTotal = 0;
        foreach ($data as $value) {
            $topic = ProjectTopicModel::getOne(['id' => $value['id'], 'project_id' => $input['project_id']], ['content_ids', 'type', 'num']);
            $numTotal = $numTotal + $topic['num'];
            if (!$topic) return $this->responseError('有项目不存在，请联系管理员');
            if ($topic['type'] == 0) {
                if ($value['content'] == $topic['content_ids']) {
                    $num = $num + $topic['num'];
                }
            } else {
                $content = explode(',', $value['content']);

                $ids = explode(',', $topic['content_ids']);


                $diff = array_diff($content, $ids);

                $diff1 = array_diff($ids, $content);

                if (!$diff && !$diff1) {
                    $num = $num + $topic['num'];
                }
            }
        }
        if (!$examine) {
            $examine['id'] = UserExamineModel::addData(['user_id' => $userId, 'project_id' => $input['project_id'], 'status' => 1, 'num' => $num]);
        } else {
            UserExamineModel::editData(['num' => $num], ['id' => $examine['id']]);
        }

        $status = 1;
        if ($num >= $info['num']) {
            UserExamineModel::editData(['status' => 2], ['id' => $examine['id']]);
            $status = 2;
        }

        $topic = ProjectTopicModel::getColumn(['project_id' => $input['project_id'], 'status' => 1], 'title', ['sort' => 'ASC', 'id' => 'DESC']);
        foreach ($topic as $key => &$value) {
            $value = ++$key . '.' . $value;
        }

        return $this->responseSuccess('请求成功', ['status' => $status, 'project_id' => $input['project_id']]);
    }

    public function examineDetail()
    {
        $input = $this->input;

        $userId = $input['user']['id'] ?? 1;

        $info = ProjectModel::getOne(['id' => $input['project_id']], ['id', 'num']);

        if (!$info) return $this->responseError('项目不存在');

        $examine = UserExamineModel::getOne(['user_id' => $userId, 'project_id' => $input['project_id']], ['id', 'status', 'num']);

        if (empty($examine)) return $this->responseError('没找到考核成绩');

        $topic = ProjectTopicModel::getList(['project_id' => $input['project_id'], 'status' => 1], ['title', 'num'], ['sort' => 'ASC', 'id' => 'DESC']);
        $numTotal = 0;
        $topicData = [];
        foreach ($topic as $key => $value) {
            $topicData[] = ++$key . '.' . $value['title'];
            $numTotal = $numTotal + $value['num'];
        }

        return $this->responseSuccess('请求成功', ['num' => $examine['num'], 'status' => $examine['status'], 'project_num' => $info['num'], 'num_total' => $numTotal, 'topic' => $topicData]);

    }


}